package com.ruoyi.basedata.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.basedata.domain.MeterSheets;
import com.ruoyi.basedata.domain.TerminalType;
import com.ruoyi.basedata.mapper.MeterSheetsMapper;
import com.ruoyi.basedata.mapper.TerminalTypeMapper;
import com.ruoyi.common.utils.DateUtils;
import jdk.nashorn.internal.ir.Terminal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.basedata.mapper.TerminalsXbViewMapper;
import com.ruoyi.basedata.domain.TerminalsXbView;
import com.ruoyi.basedata.service.ITerminalsXbViewService;

/**
 * 小表档案数据视图Service业务层处理
 *
 * @author gdqt
 * @date 2024-03-19
 */
@Service
public class TerminalsXbViewServiceImpl implements ITerminalsXbViewService {
    @Autowired
    private TerminalsXbViewMapper terminalsXbViewMapper;

    @Autowired
    private MeterSheetsMapper meterSheetsMapper;

    @Autowired
    private TerminalTypeMapper terminalTypeMapper;

    /**
     * 查询小表档案数据视图
     *
     * @param tenantId 小表档案数据视图主键
     * @return 小表档案数据视图
     */
    @Override
    public TerminalsXbView selectTerminalsXbViewByTenantId(String tenantId) {
        return terminalsXbViewMapper.selectTerminalsXbViewByTenantId(tenantId);
    }

    /**
     * 查询小表档案数据视图列表
     *
     * @param terminalsXbView 小表档案数据视图
     * @return 小表档案数据视图
     */
    @Override
    public List<TerminalsXbView> selectTerminalsXbViewList(TerminalsXbView terminalsXbView) {
        return terminalsXbViewMapper.selectTerminalsXbViewList(terminalsXbView);
    }

    /**
     * 新增小表档案数据视图
     *
     * @param terminalsXbView 小表档案数据视图
     * @return 结果
     */
    @Override
    public int insertTerminalsXbView(TerminalsXbView terminalsXbView) {
        terminalsXbView.setCreateTime(DateUtils.getNowDate());
        return terminalsXbViewMapper.insertTerminalsXbView(terminalsXbView);
    }

    /**
     * 修改小表档案数据视图
     *
     * @param terminalsXbView 小表档案数据视图
     * @return 结果
     */
    @Override
    public int updateTerminalsXbView(TerminalsXbView terminalsXbView) {
        return terminalsXbViewMapper.updateTerminalsXbView(terminalsXbView);
    }

    /**
     * 批量删除小表档案数据视图
     *
     * @param tenantIds 需要删除的小表档案数据视图主键
     * @return 结果
     */
    @Override
    public int deleteTerminalsXbViewByTenantIds(String[] tenantIds) {
        return terminalsXbViewMapper.deleteTerminalsXbViewByTenantIds(tenantIds);
    }

    /**
     * 删除小表档案数据视图信息
     *
     * @param tenantId 小表档案数据视图主键
     * @return 结果
     */
    @Override
    public int deleteTerminalsXbViewByTenantId(String tenantId) {
        return terminalsXbViewMapper.deleteTerminalsXbViewByTenantId(tenantId);
    }

    @Override
    public TerminalsXbView getTerminalsXbViewData(TerminalsXbView terminalsXbView) {
        TerminalsXbView result = terminalsXbViewMapper.selectOne(new QueryWrapper<TerminalsXbView>().eq("location_nb", terminalsXbView.getLocationNb()));

        if (result != null) {
            MeterSheets sheets = meterSheetsMapper.selectOne(new QueryWrapper<MeterSheets>().eq("sheets_code", result.getSheetsCode()));
            if (sheets != null) {
                result.setSheetsName(sheets.getSheetsName());
            }
            if (result.getTypeId() != null) {
                TerminalType terminalType = terminalTypeMapper.selectTerminalTypeById(Long.valueOf(result.getTypeId()));
                if (terminalType != null) {
                    result.setSampleTableImage(terminalType.getSampleTableImage());
                }
            }
        }

        return result;
    }
}
